package com.wang.vedio.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.fragment.NewLazyFragment;
import com.wang.vedio.MainActivity2;
import com.wang.vedio.R;
import com.wang.vedio.adapter.index.GameAdapter;
import com.wang.vedio.base.TcWebActivity;
import com.wang.vedio.bean.BannerBean;
import com.wang.vedio.event.EventTag;
import com.wang.vedio.event.MessageEvent;
import com.wang.vedio.net.entity.BaseBean;
import com.wang.vedio.net.rxjava.HttpMethods;
import com.wang.vedio.net.subscribers.ProgressSubscriber;
import com.wang.vedio.net.subscribers.SubscriberOnNextListener;
import com.wang.vedio.player.InviteFriendActivity;
import com.wang.vedio.view.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：线索发现
 */
public class GameFragment extends NewLazyFragment implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.titleLinear)
    View titleLinear;
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    Unbinder unbinder;
    private GameAdapter gameAdapter;

    @BindView(R.id.qianDaoBtn)
    View qianDaoBtn;
    @BindView(R.id.hasQianDaoTiShi)
    View hasQianDaoTiShi;

    @Override
    protected int getContentViewId() {
        return R.layout.gyss_fragment_youxi_layout;
    }

    @Override
    protected void initView(View view) {
        unbinder = ButterKnife.bind(this, view);
        titleLinear.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0, 0);
        init();
        initSrl();
        super.initView(view);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MessageEvent messageEvent) {
        if (messageEvent.getTag().equals(EventTag.DT)) {
            refresh();
        }
    }

    public void refresh() {
        srlAll.setEnableLoadMore(true);
        isShowDialog = false;
        page = 1;
        srlAll.setEnableLoadMore(true);
        mList.clear();
        getData();
    }

    private Context context;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    protected void initData() {
    }

    private void getData(){
        
        SubscriberOnNextListener<BaseBean<List<BannerBean>>> onNextListener = baseBean -> {
            
            if (srlAll != null) {
                srlAll.finishLoadMore(true);
                srlAll.finishRefresh(true);
            }
            mList.addAll(baseBean.getData());
            gameAdapter.notifyDataSetChanged();
        };
        Map<String,Object> params = new HashMap<>();
        params.put("pageIndex",""+page);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().getGameList(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

    private void initSrl() {
        srlAll.setEnableRefresh(true);
        srlAll.setEnableLoadMore(true);
        srlAll.setEnableAutoLoadMore(true);
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
            isShowDialog = false;
            page++;
            getData();
        });
    }

    private List<BannerBean> mList;

    public void setCouldClick(boolean show){
        hasQianDaoTiShi.setVisibility(show?View.VISIBLE:View.GONE);
    }

    private void init() {
        mList = new ArrayList<>();
        ((SimpleItemAnimator) recMain.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recMain.setLayoutManager(linearLayoutManager);
        gameAdapter = new GameAdapter(R.layout.item_game_list, mList);
        recMain.setAdapter(gameAdapter);
        gameAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //广告
                Bundle bundle = new Bundle();
                bundle.putString("title",mList.get(position).getTitle());
                bundle.putString("url",mList.get(position).getAdPath());
                Log.i("URLXXX",mList.get(position).getAdPath());
                RxActivityTool.skipActivity(mContext, TcWebActivity.class,bundle);
            }
        });
    }

    private int page = 1;
    private boolean isShowDialog = true;

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle = new Bundle();
    }

    @Override
    public void onResume() {
        super.onResume();
        setCouldClick(((MainActivity2)getActivity()).isCanSign());
        refresh();
    }

    @OnClick({R.id.qianDaoBtn})
    public void OnClick(View v){
        if(v.getId() == R.id.qianDaoBtn){
            Bundle bundle = new Bundle();
            bundle.putInt("type",0);
            RxActivityTool.skipActivity(mContext, InviteFriendActivity.class,bundle);
        }
    }
}